import { JwtService } from '@nestjs/jwt';
import { Injectable } from '@nestjs/common';
import { ManagerEntity } from '@/entity/manager.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { config } from '@/config';

@Injectable()
export class AuthService {
  constructor(
    @InjectRepository(ManagerEntity)
    private readonly managerRepository: Repository<ManagerEntity>,
    private readonly jwtService: JwtService,
  ) {}

  //
  createToken(name: string, password: string) {
    const accessToken = this.jwtService.sign({ name, password });
    return {
      expires_in: config.jwt.expire,
      access_token: accessToken,
    };
  }

  async validateUser(name: string, password: string): Promise<any> {
    //   这里要调用entity的方法
    return await this.managerRepository.findOne({
      where: {
        name,
        password,
      },
    });
  }
}
